<?php

namespace common\app;

use yii\helpers\Url;

include_once __DIR__ . '/user.php';
include_once __DIR__ . '/log.php';
include_once __DIR__ . '/authcode.php';

class App
{
    /**
     * @var User
     */
    public $user;


    /**
     * @var Log
     */
    public $log;

    /**
     * @var Authcode
     */
    public $authCode;

    function __construct()
    {
        $this->user = new User();
        $this->log = new Log();
        $this->authCode = new Authcode();
    }


    public function reflectionAuth()
    {
        include_once __DIR__ . '/reflectionauth.php';
        return new ReflectionAuth();
    }

    public function url($route)
    {
        return Url::to($route);
    }


    function randStr($len = 32)
    {
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= chr(mt_rand(33, 126));
        }
        return $str;
    }

    function limitGet($route, $value = '', $key = 'action')
    {
        $method = 'get';
        if (\Cm::$app->user->isGuest())
            return false;

        $account = \Cm::$app->user->getIdentity();
        
        if($account['is_superman'])
            return true;

        $accountID = $account['id'];
        $roleWhere = " AND c.role_id IN (SELECT role_id FROM sys_manager_role WHERE manager_id={$accountID}) ";

        $key = empty($value) ? '' : $key;

        $sql = "SELECT count(*) AS num 
                FROM `sys_auth_module_controller_actions` a 
                INNER JOIN sys_auth_module_controller_action_permissions b ON a.id=b.action_id
                INNER JOIN sys_auth_role_permissions c ON b.id=c.permissions_id
                WHERE a.route='{$route}' AND a.`status`=1 AND a.deprecated=0 AND b.`status`=1 AND b.deprecated=0 AND b.req_key='{$key}' AND b.req_method='{$method}' AND b.req_value='{$value}' {$roleWhere}";
        $row = \Cm::$db->one($sql);
        $num = isset($row['num']) ? $row['num'] : 0;
        if ($num)
            return true;

        return false;
    }

    function limitPost($route, $value, $key = 'action')
    {
        $method = 'post';
        if (\Cm::$app->user->isGuest())
            return false;

        $account = \Cm::$app->user->getIdentity();

        if($account['is_superman'])
            return true;

        $accountID = $account['id'];
        $roleWhere = " AND c.role_id IN (SELECT role_id FROM sys_manager_role WHERE manager_id={$accountID}) ";

        $sql = "SELECT count(*) AS num 
                FROM `sys_auth_module_controller_actions` a 
                INNER JOIN sys_auth_module_controller_action_permissions b ON a.id=b.action_id
                INNER JOIN sys_auth_role_permissions c ON b.id=c.permissions_id
                WHERE a.route='{$route}' AND a.`status`=1 AND a.deprecated=0 AND b.`status`=1 AND b.deprecated=0 AND b.req_key='{$key}' AND b.req_method='{$method}' AND b.req_value='{$value}' {$roleWhere} ";
        $row = \Cm::$db->one($sql);
        $num = isset($row['num']) ? $row['num'] : 0;
        if ($num)
            return true;

        return false;
    }
}

